var oper = {
    width : 600,
    height : 600,
    form : document.form1,
    jqgrid : function() {

        var fields = $("form").serializeArray();
        var jsonData = {};
        jQuery.each( fields, function(i, field){
            jsonData[field.name]=field.value;
        });

        $('\#jqGrid').jqGrid('setGridParam', {
            postData : jsonData
        }).trigger('reloadGrid');

        return false;
    },
    list : function() {
        oper.jqgrid();
    },
    view : function(id) {
        var url = '@{module}/@{crud.urlKey}/view/'+id;
        var title = '查看@{crud.name}';
        Iframe(url, this.width, this.height, title, false, false, false, EmptyFunc);
    },
    add : function() {
        var url = '@{module}/@{crud.urlKey}/add';
        var title = '添加@{crud.name}';
        Iframe(url, this.width, this.height, title);
    },
    edit : function(id) {
        var url = '@{module}/@{crud.urlKey}/edit/'+id;
        var title = '修改@{crud.name}';
        Iframe(url, this.width, this.height, title);
    },
    del : function(id) {
        var title = '确认要删除该@{crud.name}信息？';
        var url = '@{module}/@{crud.urlKey}/delete/'+id;
        Confirm(title, function() {
            ajax_post(url,null, function (data) {
                if(data.code==0){
                    oper.list();
                } else {
                    ErrorInfo('操作失败：'+data.msg);
                }
            });
        });
    }
};

// 初始化
jQuery(function($) {
    //显示Menu索引
    showMenu('page_@{crud.urlKey}');

    // 加载jqgrid
    var editStr = $('\#jqGridEdit').html();
    $('\#jqGrid').jqGrid({
        url:"@{module}/@{crud.urlKey}/jqgrid",
        mtype: "POST",
        styleUI : 'Bootstrap',
        datatype: "json",
        colModel: [
            {label: "id",name: 'id',width: 75,hidden:true,key:true},
# for(entry in crud.listMap){ #
            {label: "@{entry.value.name}",name: '@{entry.value.javaKey}',width: 120,sortable:true},
# } #
            {
                name: '操作', index: '', width: 200, fixed: true, sortable: false, resize: false,
                formatter: function(cellvalue, options, rowObject) {
                    var replaceStr = "\\[id\\]";
                    var buttonStr = editStr;
                    try{
                        buttonStr = buttonStr.replace(/\r\n/g,"");
                        buttonStr = buttonStr.replace(/\n/g,"");
                        buttonStr = buttonStr.replace(new RegExp(replaceStr,'gm'),rowObject.id );
                    }catch(e) {
                        alert(e.message);
                    }
                    return buttonStr ;
                }
            }
        ],
        rownumbers: true,
        sortname: 'id',
        viewrecords: true,
        autowidth: true,
        width: 1050,
        height: 630,
        rowNum: 20,
        caption: "@{crud.name}列表",
        pager: "\#jqGridPager"
    });

    // 宽高自适应
    $("\#jqGrid").setGridHeight($(window).height() - 250);
    $("\#tree").height($(window).height() - 150);
    $(window).resize(function(){
        $(window).unbind("onresize");
        $("\#jqGrid").setGridHeight($(window).height() - 250).jqGrid('setGridWidth', $('\#data_content').width() - 5);
        $("\#tree").height($(window).height() - 150);
        $(window).bind("onresize", this);
    });

    $('\#jqGrid').jqGrid('navGrid',"\#jqGridPager", {
        search: false, // show search button on the toolbar
        add: false,
        edit: false,
        del: false,
        refresh: true,
        view: false
    });

    $('\#jqGrid').navButtonAdd('\#jqGridPager',
    {
        buttonicon: "glyphicon-plus",
        title: "新增",
        caption: "新增",
        position: "first",
        onClickButton: function(){
            oper.add();
        }
    });
});
